"""
需求：获取B站新番列表信息
url = 'https://api.bilibili.com/pgc/season/index/result?st=1&order=3&season_version=-1&spoken_language_type=-1&area=-1&is_finish=-1&copyright=-1&season_status=-1&season_month=-1&year=-1&style_id=-1&sort=0&page=1&season_type=1&pagesize=20&type=1'
"""
import requests
import json
import pymongo
client = pymongo.MongoClient()
db = client['db_javis237']
tb = db['tb_bans']

headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36',
}
for i in range(1, 21):
    url = f'https://api.bilibili.com/pgc/season/index/result?st=1&order=3&season_version=-1&spoken_language_type=-1&area=-1&is_finish=-1&copyright=-1&season_status=-1&season_month=-1&year=-1&style_id=-1&sort=0&page={i}&season_type=1&pagesize=20&type=1'
    res = requests.get(url, headers=headers)
    print(res.text)
    # 把字符串转成json格式的数据
    json_data = json.loads(res.content.decode())
    print(json_data)
    rows = json_data['data']['list']
    for row in rows:
        # print(row['title'])
        # print(row['index_show'])
        # print(row['order'])
        # print(row['link'])
        data = {'title': row['title'], "show": row['index_show'], "order": row['order'], "link": row['link']}
        tb.insert_one(data)
        print(data)

# 关闭资源，释放连接
client.close()